var http = require('http');
var url = require('url');
var querystring = require('querystring');
var names = [
    "吃鸡霸王",
    "全队都爱98K",
    "打爆对面4条狗",
    "盒子精战队",
    "、s.du天娇团",
    "、姐、独占天下",
    "、豆豆部落",
    "一杆狙打天下",
    "、开门送快递的",
    "快递员丶xx",
    "乡巴佬",
    "贫民窟艺术家",
    "狂拽e族灬战队",
    "专杀丶菜鸡",
    "、反手就是一枪",
    "、致命98k",
    "- 傻逼娃子军队",
    "少年先疯队",
    "China特种部队",
    "、我是钉子，我怕个锤子"
];
var server = http.createServer((req,res)=>{
    res.setHeader("Access-Control-Allow-Origin","*");
    res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
    if(req.method.toLowerCase() == 'get'){
        let pamrs = url.parse(req.url).query;
        console.log("get请求",querystring.parse(pamrs));
    }if (req.method.toLowerCase() == 'post'){
        let dataStr = "";
        req.on("data",function(chunk){
            dataStr += chunk;
        });
        req.on("end",function(){
            console.log(dataStr);
            console.log("postcls请求",querystring.parse(dataStr));
        })
    }
    var obj = {};
    var arr = [];
    for(var i=0; i< names.length; i++){
        var c_obj = {};
        c_obj.name = names[i];
        c_obj.age = i + Math.random()*100;
        arr.push(c_obj);
    }
    obj.data = arr;
    obj.message = "请求成功";
    res.write(JSON.stringify(obj));
    res.end();
});
server.listen(8089);
console.log('server runing at localhost:8089');